<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header">
    <h1>
        库存盘点导入
    </h1>
</section>
<section class="content">
    <form id="form">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">Excel信息导入</h3>
                    </div>
                    <div class="box-body">
                        <div class="form-group">
                            <label>上次导入错误数据</label>
                            <input type="hidden" id="cpnId" name="cpnId" th:value="${checkPlan.cpnId}">
                            <a id="download-error"
                               th:data-url="@{/files/{cpnCode}/inventoryFailed.xls(cpnCode=${checkPlan.cpnCode})}"
                               href="javascript:void(0);">点击下载</a>
                        </div>
                        <div class="form-group">
                            <label>盘亏量大于库存在库量数据</label>
                            <a id="download-loss" th:href="@{/material/checkPlan/lossExport}">点击下载</a>
                        </div>
                        <div class="form-group">
                            <label>库存盘点编号</label>
                            <input type="text" readonly="readonly" class="form-control" th:value="${checkPlan.cpnCode}">
                        </div>
                        <div class="form-group">
                            <label for="upload_file">选择Excel文件并上传</label>
                            <input type="file" multiple="multiple" id="upload_file" name="file">

                            <p class="help-block">上传文档从第二行开始导入，请按格式要求整理，有‘禁改’字样列的信息不允许修改，否则将导致导入失败！</p>
                        </div>
                    </div>
                    <div class="box-footer">
                        <p class="text-red">Excel中商品盘点数量为空的将不做导入，支持小数点后最多2位.</p>
                        <button id="btn-upload" type="button" class="btn btn-primary">上传</button>
                    </div>
                </div>
            </div>
        </div>
    </form>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    var url = $("#download-loss").attr("href")

    $(function () {

        /** 下载盘亏量大于库存在库量数据 */
        $("#download-loss").click(function () {
            var cpnId = $("#cpnId").val();
            var href = url + "?cpnId=" + cpnId
            $(this).attr("href", href);
        });

        /** 下载导入失败数据 */
        $("#download-error").click(function () {
            var that = $(this);

            js.post({
                url: ctx + "material/checkPlan/exist",
                data: {
                    url: that.attr("data-url")
                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        that.attr("href", that.attr("data-url") + "?timeStamp=" + new Date().getTime());
                    } else {
                        //还没有需要处理的文件
                        js.modal.warning("还没有需要处理的文件");
                    }
                    js.modal.closeLoading();
                }
            });
        });

        /** 验证 */
        js.validate.init("form", {
            fields: {
                file: {
                    validators: {
                        notEmpty: {message: '上传文件不能为空'},
                        file: {extension: 'xls,xlsx', message: '请选择excel文件'}
                    }
                }
            }
        });

        /** 上传文件 */
        $("#btn-upload").click(function () {
            js.validSubmit({
                formId: "form",
                url: ctx + "material/checkPlan/importFile",
                data: new FormData($("#form")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.alert(result.msg);
                    } else {
                        js.modal.alert("部分数据导入失败，请下载错误数据。");
                    }
                    js.modal.closeLoading();
                },
                error: function () {
                    js.modal.alert("部分数据导入失败，请下载错误数据。");
                    js.modal.closeLoading();
                }
            });
        });

    });


</script>
</body>
</html>
